﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using AdvUtils;
using GeoUtils;

namespace BuildTRStationData
{
    class Program
    {
        static void Main(string[] args)
        {
            //RewriteAddress rewriteAddress = new RewriteAddress();
            //rewriteAddress.Initialize();

            if (args.Length != 2)
            {
                Console.WriteLine("BuildTRStationData [raw TRStation data file name] [Encoded TRStation file name]");
                return;
            }
            StreamReader sr = new StreamReader(args[0]);
            //StreamWriter sw = new StreamWriter(args[1] + ".dat");
            StreamWriter sw_raw_list = new StreamWriter(args[1] + "_raw.list");

            int maxId = 0;
            SortedDictionary<string, int> sdict = new SortedDictionary<string, int>(StringComparer.Ordinal);
            //VarBigArray<string> array = new VarBigArray<string>(1024);
            GeoEntityStorage geoItemStorage = new GeoEntityStorage();

            while (sr.EndOfStream == false)
            {
                string strLine = sr.ReadLine();
                if (maxId % 10000 == 0)
                {
                    Console.WriteLine(strLine);
                }

                //[MasterKey] [int] NOT NULL,
                //[TRLine_MasterKey] [int] NULL,
                //[Name] [nvarchar](128) NOT NULL,
                //[Alias] [nvarchar](128) NULL,
                //[Admin_Code] [varchar](12) NOT NULL,
                //[Type] [int] NOT NULL,
                //[IsCluster] [bit] NOT NULL,
                //[X_Coord] [float] NOT NULL,
                //[Y_Coord] [float] NOT NULL,
                //[Status] [smallint] NOT NULL,
                //[Create_Time] [datetime] NOT NULL,
                //[Update_Time] [datetime] NOT NULL,

                string[] items = strLine.Split('\t');
                string strName = items[2].Replace("\0", "").Trim();
                string strAlias = items[3].Replace("\0", "").Trim();
                if (strName == strAlias)
                {
                    strAlias = "";
                }

                double fX_Coord = double.Parse(items[7]);
                double fY_Coord = double.Parse(items[8]);
                LatLong ll = new LatLong(fY_Coord, fX_Coord);
                int iAdminCode = int.Parse(items[4]);

                if (sdict.ContainsKey(strName) == true)
                {
                    int idx = sdict[strName];
                    List<int> adList = new List<int>();
                    adList.Add(iAdminCode);
                    geoItemStorage.AddNewItemAt(idx, strName, "DICT_LOC_TRStation", false, adList, 7, ll);
                }
                else
                {
                    sdict.Add(strName, maxId);
                    List<int> adList = new List<int>();
                    adList.Add(iAdminCode);
                    geoItemStorage.AddNewItem(strName, "DICT_LOC_TRStation", false, adList, 7, ll);
                    maxId++;
                }

                //List<string> rewriteList = rewriteAddress.RewriteName(strName);
                //foreach (string item in rewriteList)
                //{
                //    if (item.Length == 0)
                //    {
                //        continue;
                //    }

                //    if (sdict.ContainsKey(item) == true)
                //    {
                //        int idx = sdict[item];
                //        array[idx] = array[idx] + "\t" + strInfo;
                //    }
                //    else
                //    {
                //        sdict.Add(item, maxId);
                //        array[maxId] = strInfo;
                //        maxId++;
                //    }
                //}

                if (strAlias.Length > 0)
                {
                    string[] subitems = strAlias.Split('^');
                    foreach (string si in subitems)
                    {
                        if (si.Length <= 1)
                        {
                            continue;
                        }

                        if (sdict.ContainsKey(si) == true)
                        {
                            int idx = sdict[si];
                            List<int> adList = new List<int>();
                            adList.Add(iAdminCode);
                            geoItemStorage.AddNewItemAt(idx, si, "DICT_LOC_TRStation", false, adList, 7, ll);
                        }
                        else
                        {
                            sdict.Add(si, maxId);
                            List<int> adList = new List<int>();
                            adList.Add(iAdminCode);
                            geoItemStorage.AddNewItem(si, "DICT_LOC_TRStation", false, adList, 7, ll);
                            maxId++;
                        }
                    }
                }
            }

            geoItemStorage.SaveIntoFile(args[1] + ".dat");
            sr.Close();


            foreach (KeyValuePair<string, int> pair in sdict)
            {
                sw_raw_list.WriteLine("{0}\tDICT_LOC_TRStation", pair.Key);
            }

            sw_raw_list.Close();

            DoubleArrayTrieBuilder daBuilder = new DoubleArrayTrieBuilder(4);
            daBuilder.build(sdict);
            daBuilder.save(args[1] + ".dart");

            Console.WriteLine("Build road successed");
        }
    }
}
